import { Entity, PrimaryGeneratedColumn, Column,Index, Timestamp, OneToOne, JoinColumn, OneToMany } from 'typeorm';
import { User } from './user.entity';
import { GlobalConfig } from '../../config/global/global_config';
import { Effect } from './effect/effect';
import { FightService } from '../services/fight_service';
import { Attack } from './effect/attack';
import { EffectType } from '../consts/effect_type';
import { Equipm } from './equipms/equipm.entity';
import { Weapon } from './equipms/weapon';
import { EquipmType } from '../consts/Equipm_type';
import { Shield } from './equipms/shield';
import { Unit } from './unit';
import { UnitType } from '../consts/unit_type';
import { Treasure } from './treasure.entity';
import { QualityType } from '../consts/quality_type';
import { EquipmService } from '../services/equipm_service';

var playerConfig=GlobalConfig.getFirst(GlobalConfig.ins().player);
 

@Entity("player")
export class Player extends Unit{

  @PrimaryGeneratedColumn()
  id: number;

  @OneToOne(type => User, user => user.player, {
    cascade: true
})
  user: User;

  @OneToMany(() => Treasure, treasure => treasure.player,{ cascade: true,lazy:true})
  treasures: Promise<Treasure[]>|Treasure[];

  userId:string;

  // @Column()
  // win:number=0;

  // @Column()
  // lose:number=0;

  @Column()
  level:number=1;

  @Column()
  curExp:number=0;

  @Column()
  money:number=0;

  exp:number;
  
  name:string;//玩家名称

  avatarUrl:string;//玩家头像

 
  maxAngerValue:number;//最大怒气

  curAngerValue:number;//当前怒气


  maxActionValue:number;//最大体力

  curActionValue:number;//当前体力
 
  hitRate:number;//命中率

  missRate:number;//闪避率
 
 

  isPlayer:boolean;

  //[0]=weapon,[1]=shield
  @OneToMany(() => Equipm, equipm => equipm.player,{ cascade: true,eager:true})
  equipms: Equipm[];

   

  // set curHp(value){
  //     // console.log("attack..hp="+value);
  //     if(value<=0){
  //       this._curHp=0;
  //       this.isDied=true;
  //       FightService.ins().getFight(this.id).end();
  //     }
  //     else{
  //       this._curHp=value;
  //     }
  // }

  // get curHp(){
  //   return this._curHp;
  // }
 
  

 

   init(){
    this.isDied=false;
    this.unitId=""+this.id;

    let weapon:Weapon;
    let shield:Shield;
    console.log(this.equipms);
    if(!this.equipms||this.equipms.length===0){
        this.equipms=[];
         weapon=<Weapon>EquipmService.ins().weapons[this.level][QualityType.白];
         shield=<Shield>EquipmService.ins().shields[this.level][QualityType.白];
        this.equipms[0]=weapon;
        this.equipms[1]=shield;
    }
    else{
      weapon=<Weapon>this.equipms[0];
      shield=<Shield>this.equipms[1];
      weapon.attack=EquipmService.ins().weapons[weapon.level][weapon.qualityType].attack;
      weapon.realAttack=GlobalConfig.ins().player[String(this.level)].baseAttack*weapon.attack;
      shield.hp=EquipmService.ins().shields[shield.level][shield.qualityType].hp;
      shield.realHp=GlobalConfig.ins().player[String(this.level)].baseHp*shield.hp;
    }
   
     
    this.maxHp=GlobalConfig.ins().player[String(this.level)].baseHp+shield.realHp;
    // console.log("maxhp..",GlobalConfig.ins().player[String(this.level)].baseHp,this.equipms[EquipmType.Shield].hp)
    this.setCurHp(this.maxHp);
    this.attack=GlobalConfig.ins().player[String(this.level)].baseAttack+weapon.realAttack;
    this.exp=GlobalConfig.ins().player[String(this.level)].exp;
    this.attackOverTime= Date.now();
    this.unitType=UnitType.玩家;
    this.pushId=this.userId;
    
  }

   changeEquipm(equipm:Equipm):any{
     switch(equipm.type){
       case EquipmType.Shield:
       let shield:Shield=<Shield>this.equipms[1];
       shield.level=equipm.level;
       shield.hp=Number((<Shield>equipm).hp);
       shield.imgUrl=equipm.imgUrl;
       shield.imgName=equipm.imgName;
       shield.qualityType=equipm.qualityType;
       shield.realHp=GlobalConfig.ins().player[String(this.level)].baseHp*shield.hp;
       this.maxHp=GlobalConfig.ins().player[String(this.level)].baseHp+shield.realHp;
       return shield;
       break;
       case EquipmType.Weapon:
       let weapon:Weapon=<Weapon>this.equipms[0];
       weapon.level=equipm.level;
       weapon.attack=Number((<Weapon>equipm).attack);
       weapon.imgUrl=equipm.imgUrl;
       weapon.imgName=equipm.imgName;
       weapon.qualityType=equipm.qualityType;
       weapon.realAttack=GlobalConfig.ins().player[String(this.level)].baseAttack*weapon.attack;
       this.attack=GlobalConfig.ins().player[String(this.level)].baseAttack+weapon.realAttack;
       return weapon
       break;
     }
     return null;
     
  }

  

  

}